package com.baizhi.cmfzmanager.service.impl;

import com.baizhi.cmfzmanager.dao.ArticleDao;
import com.baizhi.cmfzmanager.entity.Article;
import com.baizhi.cmfzmanager.entity.ArticleDTO;
import com.baizhi.cmfzmanager.service.ArticleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;

    @Override
    public Map getArticles(int page, int rows) {

        IPage<Article> iPage=new Page<>(page,rows);

        IPage<Article> articleIPage = articleDao.selectPage(iPage, null);
        Map map = new HashMap();
        map.put("total",articleIPage.getTotal());
        map.put("rows",articleIPage.getRecords());
        return map;
    }

    @Override
    public Map getArticlesByGuruId(int page, int rows, int guruId) {
        IPage<Article> iPage=new Page<>((page-1)*rows,rows);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("guru_id",guruId);
        IPage<Article> articleIPage = articleDao.selectPage(iPage, queryWrapper);
        Map map = new HashMap();
        map.put("total",articleIPage.getTotal());
        map.put("rows",articleIPage.getRecords());
        return map;
    }

    @Override
    public void insert(Article article) {
        articleDao.insert(article);
    }

    @Override
    public void delete(int id) {
        articleDao.deleteById(id);
    }

    @Override
    public void update(Article article) {
        articleDao.updateById(article);
    }

    @Override
    public List<ArticleDTO> getTwoArticle() {

        return articleDao.getTwoArticle();
    }

    @Override
    public List<ArticleDTO> getAllArticle() {

        return articleDao.getAllArticle();
    }

    @Override
    public List<ArticleDTO> getArticleByGuruId(Integer guruId) {

        return articleDao.getArticleByGuruId(guruId);
    }
}
